import { http } from "@/utils/http";
import type { Eviction, Pod, PodList } from "./types";
import type { BaseResponse, PDataRes, PromiseRes } from "@/api/response";
import {
  k8sPath,
  k8sPathMergeReqs,
  type MakePathReq,
  type MergePathReq
} from "./apiHelper";
import type { SourceReq } from "./protocol";

export class K8sPod {
  baseReq: MakePathReq = {
    src: "pods",
    app: ""
  };

  getPodList = (query: MergePathReq): BaseResponse<PodList> => {
    const api = k8sPathMergeReqs(query, this.baseReq);

    return http.get(api, { params: query.query });
  };

  getPodContainer = (query: MergePathReq): Promise<PDataRes<Pod>> => {
    return http.get("/api/v1/kubeHud/k8s/pod/container", { params: query });
  };

  // getPodLogUrl = (query: MergePathReq): string => {
  //   // return `/api/v1/kubeHud/k8s/pod/${query.namespace}/${query.pod}/${query.container}/log?follow=${query.follow || false}&tail=${query.tail || 100}`;
  // };

  // getPodTerminalUrl = (query: MergePathReq): string => {
  //   return `/api/v1/kubeHud/k8s/pod/${query.namespace}/${query.pod}/${query.container}/exec`;
  // };

  getPodDetail = (query?: MergePathReq, data?: Pod): BaseResponse<Pod> => {
    const api = k8sPathMergeReqs(query, this.baseReq, data);
    return http.get(api);
  };

  updatePod = (query?: MergePathReq, data?: Pod): PromiseRes => {
    const api = k8sPathMergeReqs(query, this.baseReq, data);

    return http.put(api, { data });
  };

  deletePod = (query?: MergePathReq, data?: Pod): PromiseRes => {
    const api = k8sPathMergeReqs(query, this.baseReq, data);

    return http.delete(api, { data });
  };

  createPod = (query?: MergePathReq, data?: Pod): PromiseRes => {
    const api = k8sPathMergeReqs(query, this.baseReq, data);

    return http.post(api, { data });
  };

  evictionPod = (data: Eviction): BaseResponse<any> => {
    const url = k8sPathMergeReqs({ operation: "eviction" }, this.baseReq, data);

    return http.post(url, { data });
  };

  listPodMetrics = (req: MergePathReq): BaseResponse<any> => {
    const api = k8sPathMergeReqs(req, {
      src: "pods",
      app: "metrics.k8s.io/v1beta1",
      namespace: null
    });

    return http.get(api, { params: req.query });
  };
}
